package moonstruckdrops.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import moonstruckdrops.dto.ComentDto;

public class ComentDao implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	public void NewComentSave(ComentDto NewComent){
		PersistenceManager	pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistent(NewComent);
		}finally{
			pm.close();
		}
	}
	
	public List<ComentDto> getComent(Long ID){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<ComentDto> result;
		ArrayList<ComentDto> EntryData=new ArrayList<ComentDto>();
		try{
			Query query = pm.newQuery(ComentDto.class);
			if(ID!=null){
				query.setFilter("EntryID == ID");
				query.declareParameters("Long ID");
			}
			result = (List<ComentDto>)query.execute(ID);
			pm.detachCopyAll(result);
		}finally{
			pm.close();
		}
		if(result.size()!=0){
			for(ComentDto dlist : result){
				EntryData.add(new ComentDto(dlist.getUserName(),dlist.getDate(),dlist.getComent()));
			}
		}else{
			EntryData.add(new ComentDto());
		}
		return EntryData;
	}
}